package com.cx.po;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 用户只需拥有权限
 *
 * 用户角色----控制菜单
 * 用户权限----控制方法
 *
 * Author:CHENXIAOYI
 * Since:2020/12/25 13:58
 */
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysUser implements UserDetails, CredentialsContainer, Serializable {

    private Long id;

    @JsonIgnore
    private int sex;

    private String username;

    private String password;

    private String address;

    /**
     * 权限的集合
     */
    private List<Long> auths;

    /**
     * 清空凭证的方法
     */
    @Override
    public void eraseCredentials() {
        setPassword(null);
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        auths.forEach(r-> authorityList.add(new SimpleGrantedAuthority(r.toString())));
        return authorityList;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
